﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace BookManageSystem
{
    public partial class FormBookInformationForAdministrator : Form
    {
        public FormBookInformationForAdministrator()
        {
            InitializeComponent();
        }

        //图书信息 用于修改图书信息
      
        public static string select_bookid;



        private void button3_Click(object sender, EventArgs e)
        {//修改图书信息按钮
            if (Label_ISBN.Text == "NULL")
            {
                MessageBox.Show("请选择图书！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                FormUpdateBook form = new FormUpdateBook();
                form.ShowDialog();
            }
            
        }

        private void LoadBooks()
        {//加载图书数据
            dgvBook.Rows.Clear();
            DaoAdmin dao= new DaoAdmin();
            dao.connect();

            string sql = $"select * from V_UserBookInfor";

            //准备读取器
            SqlDataReader reader = dao.read(sql);
            //while循环驱动读取器读取数据
            while (reader.Read())
            {
                string bookstatus = "";
                if (reader[4].ToString() == "0")
                {
                    bookstatus = "在馆";
                }
                else
                {
                    bookstatus = "已借出";
                }
                //一行一行加进去
                dgvBook.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(),
                    reader[3].ToString(), bookstatus, reader[5].ToString()
                    );

            }
            reader.Close();
            dao.DaoClose();
        }

        private void FormManageBooks_Load(object sender, EventArgs e)
        {

            //将移送清单中的数据显示到datagridview中
            LoadBooks();
        }

        private void dgvBook_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取当前选中的行
            if (dgvBook.CurrentRow == null || dgvBook.CurrentRow.Cells[0].Value == null)
            {
                //MessageBox.Show("选中无效数据！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string ISBN_tmp = dgvBook.CurrentRow.Cells[0].Value.ToString();//当前选中的图书编号
            string title = dgvBook.CurrentRow.Cells[2].Value.ToString();//当前选中的图书书名

            Label_ISBN.Text = ISBN_tmp;
            Label_Title.Text = title;

            select_bookid = ISBN_tmp;
        }

        private void button4_Click(object sender, EventArgs e)
        {//刷新数据
            LoadBooks();
            MessageBox.Show("刷新成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void Book_info_Click(object sender, EventArgs e)
        {
            
        }

        private void Label_Title_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {//搜索图书

            //拿到关键字
            string keyword = SearchtextBox.Text.Trim();

            //根据关键字查询数据库
            DaoAdmin dao = new DaoAdmin();
            dao.connect();

            string sql = $"select * from V_UserBookInfor where BookID like '%{keyword}%' or ISBN like '%{keyword}%' or BookName like '%{keyword}%' or AuthorName like '%{keyword}%' ";
            
            SqlDataReader reader = dao.read(sql);

            //清空表格
            dgvBook.Rows.Clear();

            //将查询到的数据显示到表格中
            while (reader.Read())
            {
                string bookstatus = "";
                if (reader[4].ToString() == "0")
                {
                    bookstatus = "在馆";
                }
                else
                {
                    bookstatus = "已借出";
                }
                //一行一行加进去
                dgvBook.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(),
                    reader[3].ToString(), bookstatus, reader[5].ToString()
                    );
            }
            reader.Close();
            dao.DaoClose();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //下架图书

            //获取当前选中的图书编号
            if(Label_ISBN.Text == "NULL")
            {
                MessageBox.Show("请选择图书！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DialogResult result = MessageBox.Show("确定下架该图书吗？", "确认下架", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.No)
            {
                return;
            }
            else 
            {
                //删除数据库中的数据
                DaoAdmin dao = new DaoAdmin();
                dao.connect();

                string sql = $"delete from BookInfor where BookID='{Label_ISBN.Text}'";
               
                if (dao.Execute(sql) > 0)
                {   //删除成功
                    Label_ISBN.Text = "NULL";
                    Label_Title.Text = "NULL";

                    //刷新数据
                    LoadBooks();

                    MessageBox.Show("下架该图书成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("下架该图书失败！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void dgvBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            FormAddBooks form = new FormAddBooks();
            form.ShowDialog();
        }
    }
}
